package org.wkg;

import java.sql.*;
import java.text.MessageFormat;

public class Deom01_DriverManger {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/java114?characterEncoding=utf8" +
                    "&allowPublicKeyRetrieval=true&useSSL=false ", "root", "123456");
            statement = connection.createStatement();
            String sql = "select id,name,sno,age,gender,enroll_date,class_id from student";
            resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                long id = resultSet.getLong(1);
                String name = resultSet.getString(2);
                String sno = resultSet.getString(3);
                int age = resultSet.getInt(4);
                byte gender = resultSet.getByte(5);
                Date enroll_date = resultSet.getDate(6);
                long class_id = resultSet.getLong(7);
                System.out.println(MessageFormat.format("学生编号={0}, 姓名={1}, 学号={2}, 年龄={3}, 性别={4}, " +
                                "入学时间={5}, 班级编号={6}"
                        , id, name, sno, age, gender, enroll_date, class_id));
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
